﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqUnit
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);

        public List<SalesOneUnit> GetUnits(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<UnitEntity> query = db.UnitEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "UnitId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.UnitId);
                            else
                                query = query.OrderByDescending(c => c.UnitId);
                            break;
                        case "UnitCode":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.UnitCode);
                            else
                                query = query.OrderByDescending(c => c.UnitCode);
                            break;
                        case "UnitName":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.UnitName);
                            else
                                query = query.OrderByDescending(c => c.UnitName);
                            break;
                    }
                }
                return query.Select(c => Mapper.ToUnitObject(c)).ToList();
            }
        }


        public SalesOneUnit GetUnit(int unitid)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToUnitObject(db.UnitEntities
                            .SingleOrDefault(p => p.UnitId == unitid));
            }
        }


        public List<SalesOneUnit> GetUnitsInBranch(int branchID)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<UnitEntity> query = db.UnitEntities.Where(p => p.BranchId == branchID);
                return query.Select(c => Mapper.ToUnitObject(c)).ToList();
            }
        }

        





        public List<SalesOneUnit> GetUnitsInBranch(string sortExpression, string userName)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<UnitEntity> query = db.UnitEntities;

                query = from unit in db.UnitEntities 
                        join usersInInfrastructure in db.UsersInStructureEntities on unit.UnitId equals usersInInfrastructure.NodeId
                        where usersInInfrastructure.UserTypeId == 4
                        join users in db.aspnet_Users on usersInInfrastructure.UserId equals users.UserId
                        where users.UserName == userName
                        select unit;


                return query.Select(c => Mapper.ToUnitObject(c)).ToList();
            }
        }


        public void InsertUnit(SalesOneUnit unit)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    UnitEntity entity = Mapper.ToUnitEntity(unit);
                    db.UnitEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    unit.UnitId = entity.UnitId;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert contact " + unit.Name);
                }
            }
        }


        public int UpdateUnit(SalesOneUnit unit)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    UnitEntity entity = Mapper.ToUnitEntity(unit);
                    db.UnitEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        public int DeleteUnit(SalesOneUnit unit)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    UnitEntity entity = Mapper.ToUnitEntity(unit);
                    db.UnitEntities.Attach(entity, false);
                    db.UnitEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        public List<SalesOneUnit> GetUnitsForBranch(int branchId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<UnitEntity> query = db.UnitEntities;

                query = from p
                        in db.UnitEntities
                        where p.BranchId == branchId
                        select p;

                return query.Select(c => Mapper.ToUnitObject(c)).ToList();
            }
        }


        public List<SalesOneUnit> GetUnitsForBranch(int branchId, int pageNo)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<UnitEntity> query = db.UnitEntities;

                query = from p
                        in db.UnitEntities
                        where p.BranchId == branchId
                        select p;

                IQueryable<UnitEntity> pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToUnitObject(c)).ToList();
            }
        }

       
    }
}
